package com.wei.czz.framework.common.manager;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.TypeReference;
import com.wei.czz.common.constant.EntityConstant;
import com.wei.czz.common.dto.common.NameValue;
import com.wei.czz.common.dto.common.sms.SmsLogDto;
import com.wei.czz.common.dto.common.sms.SmsLogPageEnumDto;
import com.wei.czz.common.enums.CommonEnum;
import com.wei.czz.common.enums.common.sms.SmsChannelEnum;
import com.wei.czz.common.enums.common.sms.SmsTypeEnum;
import com.wei.czz.common.tool.PageDto;
import com.wei.czz.common.utils.CacheKeyUtils;
import com.wei.czz.common.utils.CopyUtils;
import com.wei.czz.common.utils.DataUtils;
import com.wei.czz.common.vo.common.sms.SmsLogFormVo;
import com.wei.czz.framework.common.entity.SmsLogEntity;
import com.wei.czz.framework.common.helper.CacheHelper;
import com.wei.czz.framework.common.service.SmsLogService;
import com.wei.czz.framework.common.service.impl.AesServiceImpl;
import lombok.AllArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;

/**
 * Created by IntelliJ IDEA.
 *
 * @author wyw
 * date: 2024-06-18 10:49:28
 * className: SmsLogManager
 * version: 1.0
 * description:
 */
@Component
@AllArgsConstructor
public class SmsLogManager {

    private static final Logger log = LoggerFactory.getLogger(SmsLogManager.class);

    private final SmsLogService smsLogService;

    private final AesServiceImpl aesService;

    private final CacheHelper cacheHelper;


    public PageDto<SmsLogDto> getSmsLogPageList(SmsLogFormVo smsLogFormVo) {

        /*
            缓存请求参数
         */
        String cacheKey = CacheKeyUtils.getParamCacheKey(EntityConstant.SMS_LOG);
        cacheHelper.cache(cacheKey, smsLogFormVo);

        /*
            分页获取短信发送记录
         */
        PageDto<SmsLogEntity> pageDto = smsLogService.getPageList(smsLogFormVo);
        if (pageDto.isEmpty()) {
            return pageDto.rebuild();
        }

        List<SmsLogEntity> smsLogList = pageDto.getList();

        // 列表映射
        List<SmsLogDto> smsLogDtoList = CopyUtils.mapList(smsLogList, smsLog -> {
            SmsLogDto smsLogDto = CopyUtils.map(smsLog, SmsLogDto.class);
            smsLogDto.setMobile(aesService.decodeHex(smsLog.getMobile()))
                    .setSubMobile(DataUtils.dataMask(smsLogDto.getMobile()))
                    .setTypeName(SmsTypeEnum.getName(smsLog.getType()))
                    .setChannelName(SmsChannelEnum.getName(smsLog.getChannel()))
                    .setContent(aesService.czzDecrypt(smsLog.getContent()))
                    .setParamMap(Collections.emptyMap())
                    .setResultMap(Collections.emptyMap())
                    .setStatusName(
                            CommonEnum.ZERO.getValue().equals(smsLog.getStatus()) ? "发送成功" : "发送失败"
                    );
            if (StringUtils.isNotBlank(smsLog.getParam())) {
                // 请求参数解密
                String param = aesService.czzDecrypt(smsLog.getParam());
                Map<String, String> paramMap = JSON.parseObject(param, new TypeReference<Map<String, String>>() {});
                smsLogDto.setParamMap(paramMap);
            }
            if (StringUtils.isNotBlank(smsLog.getResult())) {
                String result = aesService.czzDecrypt(smsLog.getResult());
                Map<String, String> resultMap = JSON.parseObject(result, new TypeReference<Map<String, String>>() {});
                smsLogDto.setResultMap(resultMap);
            }
            return smsLogDto;
        });

        // 封装返回
        return pageDto.rebuild(smsLogDtoList);
    }

    public SmsLogPageEnumDto getSmsLogPageEnum() {

        /*
            获取请求参数
         */
        String cacheKey = CacheKeyUtils.getParamCacheKey(EntityConstant.SMS_LOG);
        SmsLogFormVo smsLogFormVo = cacheHelper.get(cacheKey, SmsLogFormVo.class);

        List<NameValue<Integer>> statusList = new ArrayList<>();
        statusList.add(new NameValue<>("发送成功", CommonEnum.ZERO.getValue()));
        statusList.add(new NameValue<>("发送失败", CommonEnum.ONE.getValue()));

        SmsLogPageEnumDto smsLogPageEnumDto = new SmsLogPageEnumDto();
        smsLogPageEnumDto.setTypeList(SmsTypeEnum.getList())
                .setChannelList(SmsChannelEnum.getList())
                .setStatusList(statusList)
                .setSmsLogFormVo(smsLogFormVo);
        return smsLogPageEnumDto;
    }

}
